/*

This file is part of Rooms3D.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
Rooms3D is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
Rooms3D is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with Rooms3D; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/

package com.datasentinel.j3d.controls.hud;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.AxisAngle4f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import com.datasentinel.j3d.controls.J3DComponent;
import com.datasentinel.j3d.controls.J3DComponentUI;

/**
 * @author rickp
 * 
 */
public class ClipboardContentsViewerUI extends J3DComponentUI
{
	private Transform3D clipboardTransform;
	private TransformGroup clipboardTG;
	public ClipboardContentsViewerUI()
	{
		super();
	}

	@Override
	protected void constructComponentGUIBranch()
	{
		super.baseBrnchGrp.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
		super.baseBrnchGrp.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
		super.baseBrnchGrp.addChild(clipboardTG);
	}

	@Override
	protected void initializeComponentParts()
	{
		clipboardTG = new TransformGroup();
		clipboardTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
		clipboardTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
	}

	@Override
	protected void initializeComponentPartsLayout()
	{
		clipboardTransform = new Transform3D();
		clipboardTransform.setTranslation(new Vector3d());
		clipboardTG.setTransform(clipboardTransform);
	}

	@Override
	public void render3DComponent(J3DComponent component)
	{
		if(component instanceof ClipboardContentsViewer)
		{
			ClipboardContentsViewer clipboardContentsViewer = (ClipboardContentsViewer)component;
			ClipboardContentsViewerModel model = clipboardContentsViewer.getModel();
			clipboardTG.removeAllChildren();
			Transform3D transform = new Transform3D();
			Vector3f translation = new Vector3f();
			for(BranchGroup selectedItem: model.getSelection())
			{
				BranchGroup boxBG = new BranchGroup();
				boxBG.setCapability(BranchGroup.ALLOW_DETACH);
				TransformGroup boxTG = new TransformGroup();
				translation.setX(translation.getX()+0.1f);
				translation.setY(translation.getY()+0.1f);
				transform.setTranslation(translation);
				AxisAngle4f rotation = new AxisAngle4f(0,1,1,0);
				rotation.setAngle((float) Math.PI/16f+0.1f);
				transform.setRotation(rotation);
				boxTG.setTransform(transform);
				((TransformGroup)selectedItem.getChild(0)).setTransform(transform);
				clipboardTG.addChild(selectedItem);
			}		
		}
	}
}
